<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class CaiNiaoTestController extends Controller
{
    // SKU/保质期 itemId => 11010001/11030001, 序列号商品 itemId ==> 11020001,
    protected $itemId = "11010001";
    //protected $itemId = "11020001";
    //protected $itemId = "11030001";

    // SKU itemCode => SKUA00001, 序列号 itemCode => SKUB00001, 保质期 itemCode => SKUC00001
    protected $itemCode = "SKUA00001";
    //protected $itemCode = "SKUB00001";
    //protected $itemCode = "SKUC00001";

    protected $orderItemId = 'ZHdev2022082100101A01';
    //protected $orderItemId = 'ZHdev2022082100101B01';
    //protected $orderItemId = 'ZHdev2022082100101C01';

    // 库房编码
    protected $storeCode = "SHA201";

    protected $qty = 1;

    protected $orderCode = '2451087806001113479';
    protected $orderCode2 = '2451087806001113480';

    protected $isDef = 0;

    protected $isHalf = 0;
    protected $halfQty = 2;

    //nohalf
    protected $manyPack = 0;
    protected $sPackQty = 1;

    //same qty
    protected $manyItem = 1;
    //protected $itemId2 = "11010002";
    //protected $itemId2 = "11020002";
    protected $itemId2 = "11030002";
    //protected $itemCode2 = "SKUA00002";
    //protected $itemCode2 = "SKUB00002";
    protected $itemCode2 = "SKUC00002";
    //protected $orderItemId2 = 'ZHdev2022082100101A02';
    //protected $orderItemId2 = 'ZHdev2022082100101B02';
    protected $orderItemId2 = 'ZHdev2022082100101C02';

    // 库存类型: 1-可销售库存, 101-残次品
    // 良品库存类型
    protected $normalInventoryType = 1;
    // 残次库存类型
    protected $defInventoryType = 101;

    public function index(Request $request)
    {
        $method = $request->get('method');
        if (empty($method)) {
            return [];
        }
        $method = str_replace('.', '_', $method);

        if (!method_exists($this, $method)) {
            echo sprintf("[%s] mock function not exists", $method);
            exit;
        }

        return $this->$method($request);
    }

    // 获取销退收货信息（taobao.wlb.wms.return.bill.get）
    public function taobao_wlb_wms_return_bill_get(Request $request)
    {
        $orderCode = $request->get('order_code', '');
        $cnOrderCode = $request->get('cn_order_code', '');

        $orderType = 501;
        $inventoryItem = [
            "inventory_item" => [
                "inventory_type" => $this->isDef ? $this->defInventoryType : $this->normalInventoryType,
                "item_qty" => $this->qty,
                "produce_code" => (string)rand(1000, 9999),
                "produce_area" => "浙江杭州",
                "batch_code" => (string)rand(100000000000000, 999999999999999),
                'produce_date' => '',
                'due_date' => '',
            ]
        ];
        if ($this->itemCode == "SKUC00001") {
            $inventoryItem['inventory_item']['produce_date'] = date("Y-m-d H:i:s", strtotime("-1 week"));
            $inventoryItem['inventory_item']['due_date'] = date("Y-m-d H:i:s", strtotime("+8 weeks"));
        }
        if ($this->isHalf) {
            $inventoryItem['inventory_item']['item_qty'] -= $this->halfQty;
        }

        if ($this->isHalf) {
            $inventoryItem2 = [
                'inventory_item' => [
                    "batch_code" => (string)rand(100000000000000, 999999999999999),
                    "produce_area" => "浙江杭州",
                    "inventory_type" => $this->isDef ? $this->normalInventoryType : $this->defInventoryType,
                    "item_qty" => $this->halfQty,
                    "produce_code" => (string)rand(1000, 9999),
                    'produce_date' => '',
                    'due_date' => '',
                ]
            ];
            if ($this->itemCode == "SKUC00001") {
                $inventoryItem2['inventory_item']['produce_date'] = date("Y-m-d H:i:s", strtotime("-1 week"));
                $inventoryItem2['inventory_item']['due_date'] = date("Y-m-d H:i:s", strtotime("+8 weeks"));
            }
        }

        $cainiao_return_bill_inventoryitemlists = [$inventoryItem];
        if ($this->isHalf) {
            $cainiao_return_bill_inventoryitemlists[] = $inventoryItem2;
        }

        $orderItem = [
            "order_item" => [
                "order_item_id" => $this->orderItemId,
                "item_id" => $this->itemId,
                "item_code" => $this->itemCode,
                "inventory_item_list" => [
                    "cainiao_return_bill_inventoryitemlist" => $cainiao_return_bill_inventoryitemlists
                ]
            ]
        ];

        if ($this->manyItem) {
            $inventory2Item = [
                "inventory_item" => [
                    "inventory_type" => $this->isDef ? $this->defInventoryType : $this->normalInventoryType,
                    "item_qty" => $this->qty,
                    "produce_code" => (string)rand(1000, 9999),
                    "produce_area" => "浙江杭州",
                    "batch_code" => (string)rand(100000000000000, 999999999999999),
                    'produce_date' => '',
                    'due_date' => '',
                ]
            ];
            if ($this->itemCode2 == "SKUC00002") {
                $inventory2Item['inventory_item']['produce_date'] = date("Y-m-d H:i:s", strtotime("-1 week"));
                $inventory2Item['inventory_item']['due_date'] = date("Y-m-d H:i:s", strtotime("+8 weeks"));
            }
            if ($this->isHalf) {
                $inventory2Item['inventory_item']['item_qty'] -= $this->halfQty;
            }

            if ($this->isHalf) {
                $inventory2Item2 = [
                    'inventory_item' => [
                        "batch_code" => (string)rand(100000000000000, 999999999999999),
                        "produce_area" => "浙江杭州",
                        "inventory_type" => $this->isDef ? $this->normalInventoryType : $this->defInventoryType,
                        "item_qty" => $this->halfQty,
                        "produce_code" => (string)rand(1000, 9999),
                        'produce_date' => '',
                        'due_date' => '',
                    ]
                ];
                if ($this->itemCode2 == "SKUC00002") {
                    $inventory2Item2['inventory_item']['produce_date'] = date("Y-m-d H:i:s", strtotime("-1 week"));
                    $inventory2Item2['inventory_item']['due_date'] = date("Y-m-d H:i:s", strtotime("+8 weeks"));
                }
            }

            $cainiao_return_bill_inventoryitemlists2 = [$inventory2Item];
            if ($this->isHalf) {
                $cainiao_return_bill_inventoryitemlists2[] = $inventory2Item2;
            }

            $orderItem2 = [
                "order_item" => [
                    "order_item_id" => $this->orderItemId2,
                    "item_id" => $this->itemId2,
                    "item_code" => $this->itemCode2,
                    "inventory_item_list" => [
                        "cainiao_return_bill_inventoryitemlist" => $cainiao_return_bill_inventoryitemlists2
                    ]
                ]
            ];
        }

        $cainiao_return_bill_orderitemlist = [$orderItem];
        if ($this->manyItem) {
            $cainiao_return_bill_orderitemlist[] = $orderItem2;
        }

        return [
            "wlb_wms_return_bill_get_response" => [
                "return_order_info" => [
                    "order_code" => !empty($orderCode) ? $orderCode : $this->orderCode,
                    "cn_order_code" => $cnOrderCode ? $cnOrderCode : sprintf("LBX%s%s", date('YmdHis'), (string)rand(100, 999)),
                    "order_type" => $orderType,
                    "confirm_time" => date("Y-m-d H:i:s"),
                    "order_item_list" => [
                        "cainiao_return_bill_orderitemlist" => $cainiao_return_bill_orderitemlist
                    ],
                    "pre_cn_order_code" => sprintf("LBX%s%s", date('YmdHis'), (string)rand(100, 999))
                ],
            ],
        ];
    }

    // 获取销售订单发货信息（taobao.wlb.wms.consign.bill.get）
    public function taobao_wlb_wms_consign_bill_get(Request $request)
    {
        $status = 'WMS_CONFIRMED';
        $orderCode = $request->get('order_code', '');
        $cnOrderCode = $request->get('cn_order_code', '');

        $inventoryitemlist = [
            'inventory_item' => [
                "batch_code" => (string)rand(100000000000000, 999999999999999),
                "produce_area" => "浙江杭州",
                "inventory_type" => $this->isDef ? $this->defInventoryType : $this->normalInventoryType,
                "item_qty" => $this->qty,
                "produce_code" => (string)rand(1000, 9999),
                'produce_date' => '',
                'due_date' => '',
            ]
        ];
        if ($this->isHalf) {
            $inventoryitemlist['inventory_item']['item_qty'] -= $this->halfQty;
        }

        if ($this->itemCode == "SKUC00001") {
            $inventoryitemlist['inventory_item']['produce_date'] = date("Y-m-d H:i:s", strtotime("-1 week"));
            $inventoryitemlist['inventory_item']['due_date'] = date("Y-m-d H:i:s", strtotime("+8 weeks"));
        }
        if ($this->isHalf) {
            $inventoryitemlist2 = [
                'inventory_item' => [
                    "batch_code" => (string)rand(100000000000000, 999999999999999),
                    "produce_area" => "浙江杭州",
                    "inventory_type" => $this->isDef ? $this->normalInventoryType : $this->defInventoryType,
                    "item_qty" => $this->halfQty,
                    "produce_code" => (string)rand(1000, 9999),
                    'produce_date' => '',
                    'due_date' => '',
                ]
            ];
            if ($this->itemCode == "SKUC00001") {
                $inventoryitemlist2['inventory_item']['produce_date'] = date("Y-m-d H:i:s", strtotime("-1 week"));
                $inventoryitemlist2['inventory_item']['due_date'] = date("Y-m-d H:i:s", strtotime("+8 weeks"));
            }
        }

        $inventoryitemlists = [$inventoryitemlist];
        if ($this->isHalf) {
            $inventoryitemlists[] = $inventoryitemlist2;
        }

        if ($this->manyItem) {
            $inventoryitem2list = [
                'inventory_item' => [
                    "batch_code" => (string)rand(100000000000000, 999999999999999),
                    "produce_area" => "浙江杭州",
                    "inventory_type" => $this->isDef ? $this->defInventoryType : $this->normalInventoryType,
                    "item_qty" => $this->qty,
                    "produce_code" => (string)rand(1000, 9999),
                    'produce_date' => '',
                    'due_date' => '',
                ]
            ];
            if ($this->isHalf) {
                $inventoryitem2list['inventory_item']['item_qty'] -= $this->halfQty;
            }

            if ($this->itemCode2 == "SKUC00002") {
                $inventoryitem2list['inventory_item']['produce_date'] = date("Y-m-d H:i:s", strtotime("-1 week"));
                $inventoryitem2list['inventory_item']['due_date'] = date("Y-m-d H:i:s", strtotime("+8 weeks"));
            }
            if ($this->isHalf) {
                $inventoryitem2list2 = [
                    'inventory_item' => [
                        "batch_code" => (string)rand(100000000000000, 999999999999999),
                        "produce_area" => "浙江杭州",
                        "inventory_type" => $this->isDef ? $this->normalInventoryType : $this->defInventoryType,
                        "item_qty" => $this->halfQty,
                        "produce_code" => (string)rand(1000, 9999),
                        'produce_date' => '',
                        'due_date' => '',
                    ]
                ];
                if ($this->itemCode2 == "SKUC00002") {
                    $inventoryitem2list2['inventory_item']['produce_date'] = date("Y-m-d H:i:s", strtotime("-1 week"));
                    $inventoryitem2list2['inventory_item']['due_date'] = date("Y-m-d H:i:s", strtotime("+8 weeks"));
                }
            }

            $inventoryitem2lists = [$inventoryitem2list];
            if ($this->isHalf) {
                $inventoryitem2lists[] = $inventoryitem2list2;
            }
        }

        $orderitemlist = [
            'order_item' => [
                "item_code" => $this->itemCode,
                "item_id" => $this->itemId,
                "order_item_id" => $this->orderItemId,
                "order_source_code" => !empty($orderCode) ? $orderCode : $this->orderCode,
                "inventory_item_list" => [
                    'inventoryitemlist' => $inventoryitemlists
                ],
            ],
        ];
        if ($this->manyItem) {
            $orderitemlist2 = [
                'order_item' => [
                    "item_code" => $this->itemCode2,
                    "item_id" => $this->itemId2,
                    "order_item_id" => $this->orderItemId2,
                    "order_source_code" => !empty($orderCode) ? $orderCode : $this->orderCode2,
                    "inventory_item_list" => [
                        'inventoryitemlist' => $inventoryitem2lists
                    ],
                ],
            ];
        }


        $tmsitemlist = [
            'tms_item' => [
                "item_qty" => $this->qty,
                "item_code" => $this->itemCode,
                "item_id" => $this->itemId,
            ]
        ];
        if ($this->manyPack) {
            $tmsitemlist['tms_item']['item_qty'] -= $this->sPackQty;
        }
        if ($this->manyPack) {
            $tmsitemlist2 = [
                'tms_item' => [
                    "item_qty" => $this->sPackQty,
                    "item_code" => $this->itemCode,
                    "item_id" => $this->itemId,
                ]
            ];
        }

        if ($this->manyItem) {
            $tmsitem2list = [
                'tms_item' => [
                    "item_qty" => $this->qty,
                    "item_code" => $this->itemCode2,
                    "item_id" => $this->itemId2,
                ]
            ];
            if ($this->manyPack) {
                $tmsitem2list['tms_item']['item_qty'] -= $this->sPackQty;
            }
            if ($this->manyPack) {
                $tmsitem2list2 = [
                    'tms_item' => [
                        "item_qty" => $this->sPackQty,
                        "item_code" => $this->itemCode2,
                        "item_id" => $this->itemId2,
                    ]
                ];
            }
        }

        $tmsitemlists = [$tmsitemlist];
        if ($this->manyItem) {
            $tmsitemlists[] = $tmsitem2list;
        }

        $tmsorderlist = [
            'tms_order' => [
                "tms_code" => "ZTO",
                "tms_order_code" => sprintf('ZTO%s%s', date('Ymd'), rand(1000, 9999)),
                "package_code" => sprintf('SHA%s%s', date('YmdHis'), strtoupper(Str::random(6))),
                "package_height" => 20,
                "package_length" => 20,
                "package_material_list" => [],
                "package_weight" => 145,
                "package_width" => 80,
                "tms_item_list" => [
                    'tmsitemlist' => $tmsitemlists
                ],
            ]
        ];
        if ($this->manyPack) {
            $tmsitemlists2 = [$tmsitemlist2];
            if ($this->manyItem) {
                $tmsitemlists2[] = $tmsitem2list2;
            }
            $tmsorderlist2 = [
                'tms_order' => [
                    "tms_code" => "ZTO",
                    "tms_order_code" => sprintf('ZTO%s%s', date('Ymd'), rand(1000, 9999)),
                    "package_code" => sprintf('SHA%s%s', date('YmdHis'), strtoupper(Str::random(6))),
                    "package_height" => 20,
                    "package_length" => 20,
                    "package_material_list" => [],
                    "package_weight" => 145,
                    "package_width" => 80,
                    "tms_item_list" => [
                        'tmsitemlist' => $tmsitemlists2
                    ],
                ]
            ];
        }

        $tms_order_list = [$tmsorderlist];
        if ($this->manyPack) {
            $tms_order_list[] = $tmsorderlist2;
        }

        $orderitemlistss = [$orderitemlist];
        if ($this->manyItem) {
            $orderitemlistss[] = $orderitemlist2;
        }
        return [
            'wlb_wms_consign_bill_get_response' => [
                'consign_send_info_list' => [
                    'consignsendinfolist' => [
                        [
                            'consign_send_info' => [
                                "status" => $status,
                                "store_code" => $this->storeCode,
                                "cn_order_code" => $cnOrderCode ? $cnOrderCode : sprintf("LBX%s%s", date('YmdHis'), (string)rand(100, 999)),
                                "confirm_time" => date('Y-m-d H:i:s'),
                                "order_code" => !empty($orderCode) ? $orderCode : $this->orderCode,
                                "order_item_list" => [
                                    'orderitemlist' => $orderitemlistss,
                                ],
                                "tms_order_list" => [
                                    'tmsorderlist' => $tms_order_list,
                                ],
                                'invoince_confirm_list' => [],
                                'order_type' => 201
                            ],
                        ]
                    ]
                ],
                'request_id' => Str::random(10),
            ],
        ];
    }

    // 商家按照仓的类型获取仓库接口 (taobao.wlb.stores.baseinfo.get)
    public function taobao_wlb_stores_baseinfo_get()
    {
        $storeInfo = [
            "real_name" => "华北仓201",
            "name" => "华北仓",
            "service_code" => "SHA201",
            "address" => "浙江省杭州市西湖区网商路699号"
        ];

        return [
            'wlb_stores_baseinfo_get_response' => [
                "store_info_list" => [
                    "store_info" => [$storeInfo]
                ],
                "total_count" => 1
            ]
        ];
    }

    // 查询单据序列号信息（taobao.wlb.wms.sn.info.query）
    public function taobao_wlb_wms_sn_info_query()
    {
        $sninfolist = [];
        if ($this->itemCode == 'SKUB00001') {
            if ($this->isHalf) {
                for ($i = 1; $i <= $this->qty - $this->halfQty; $i++) {
                    $sninfolist[] = $this->isDef ? $this->_getDefSns() : $this->_getNormalSns();
                }
                for ($i = 1; $i <= $this->halfQty; $i++) {
                    $sninfolist[] = $this->isDef ? $this->_getNormalSns() : $this->_getDefSns();
                }
            } else {
                for ($i = 1; $i <= $this->qty; $i++) {
                    $sninfolist[] = $this->isDef ? $this->_getDefSns() : $this->_getNormalSns();
                }
            }
        }
        if ($this->manyItem && $this->itemCode2 == 'SKUB00002') {
            if ($this->isHalf) {
                for ($i = 1; $i <= $this->qty - $this->halfQty; $i++) {
                    $sninfolist[] = $this->isDef ? $this->_getDefSns(true) : $this->_getNormalSns(true);
                }
                for ($i = 1; $i <= $this->halfQty; $i++) {
                    $sninfolist[] = $this->isDef ? $this->_getNormalSns(true) : $this->_getDefSns(true);
                }
            } else {
                for ($i = 1; $i <= $this->qty; $i++) {
                    $sninfolist[] = $this->isDef ? $this->_getDefSns(true) : $this->_getNormalSns(true);
                }
            }
        }
        return [
            'wlb_wms_sn_info_query_response' => [
                'result' => [
                    "success" => true,
                    "error_code" => "0",
                    "error_msg" => "",
                    "total_count" => 1,
                    "sn_info_list" => [
                        'sninfolist' => $sninfolist
                    ]
                ],
            ],
        ];
    }

    // 良品序列号
    private function _getNormalSns($sItem = false)
    {
        return [
            'sn_info' => [
                "inventory_type" => $this->normalInventoryType,
                "sn_code" => (string)rand(1000000000, 9999999999),
                "item_code" => $sItem ? $this->itemCode2 : $this->itemCode,
                "item_id" => $sItem ? $this->itemId2 : $this->itemId
            ]
        ];
    }

    // 残次序列号
    public function _getDefSns($sItem = false)
    {
        return [
            'sn_info' => [
                "inventory_type" => $this->defInventoryType,
                "sn_code" => (string)rand(1000000000, 9999999999),
                "item_code" => $sItem ? $this->itemCode2 : $this->itemCode,
                "item_id" => $sItem ? $this->itemId2 : $this->itemId
            ]
        ];
    }

    // 查询单据列表（taobao.wlb.wms.cainiao.bill.query）
    public function taobao_wlb_wms_cainiao_bill_query()
    {
        $status = 'WMS_CONFIRMED';
        $order_info = [
            'order_info' => [
                "remark" => "备注",
                "modified_time" => date('Y-m-d H:i:s'),
                "status" => $status,
                "store_code" => $this->storeCode,
                "order_type" => 201,
                "order_code" => $this->orderCode,
                "cn_order_code" => sprintf("LBX%s%s", date('YmdHis'), (string)rand(100, 999)),
                "order_source" => "201",
                "order_source_codes" => [
                    'order_source_code' => [$this->orderCode]
                ]
            ]
        ];

        return [
            'wlb_wms_cainiao_bill_query_response' => [
                'order_info_list' => [
                    'cainiao_bill_query_orderinfolist' => [
                        $order_info
                    ]
                ],
                'total_count' => 1,
            ]
        ];
    }

    // 通过订单号获取单个出库单发货信息 (taobao.wlb.wms.stock.out.bill.get)
    public function taobao_wlb_wms_stock_out_bill_get(Request $request)
    {
        $orderCode = $request->get('order_code', '');
        $cnOrderCode = $request->get('cn_order_code', '');


        $package_item = [
            "package_item" => [
                "order_item_id" => $this->orderItemId,
                "item_id" => $this->itemId,
                "item_code" => $this->itemCode,
                "inventory_type" => $this->isDef ? $this->defInventoryType : $this->normalInventoryType,
                "item_qty" => $this->qty
            ]
        ];
        if ($this->isHalf) {
            $package_item['package_item']["item_qty"] -= $this->halfQty;
        }
        if ($this->manyPack) {
            $package_item['package_item']["item_qty"] -= $this->sPackQty;
        }

        if ($this->isHalf) {
            $package_item2 = [
                "package_item" => [
                    "order_item_id" => $this->orderItemId,
                    "item_id" => $this->itemId,
                    "item_code" => $this->itemCode,
                    "inventory_type" => $this->isDef ? $this->normalInventoryType : $this->defInventoryType,
                    "item_qty" => $this->halfQty
                ]
            ];

        }

        if ($this->manyItem) {
            $package_item21 = [
                "package_item" => [
                    "order_item_id" => $this->orderItemId2,
                    "item_id" => $this->itemId2,
                    "item_code" => $this->itemCode2,
                    "inventory_type" => $this->isDef ? $this->defInventoryType : $this->normalInventoryType,
                    "item_qty" => $this->qty
                ]
            ];
            if ($this->isHalf) {
                $package_item21['package_item']["item_qty"] -= $this->halfQty;
            }
            if ($this->manyPack) {
                $package_item21['package_item']["item_qty"] -= $this->sPackQty;
            }

            if ($this->isHalf) {
                $package_item22 = [
                    "package_item" => [
                        "order_item_id" => $this->orderItemId2,
                        "item_id" => $this->itemId2,
                        "item_code" => $this->itemCode2,
                        "inventory_type" => $this->isDef ? $this->normalInventoryType : $this->defInventoryType,
                        "item_qty" => $this->halfQty
                    ]
                ];

            }
        }

        $package_items = [$package_item];
        if ($this->isHalf) {
            $package_items[] = $package_item2;
        }
        if ($this->manyItem) {
            $package_items[] = $package_item21;
            if ($this->isHalf) {
                $package_items[] = $package_item22;
            }
        }

        if ($this->manyPack) {
            $package2_item = [
                "package_item" => [
                    "order_item_id" => $this->orderItemId,
                    "item_id" => $this->itemId,
                    "item_code" => $this->itemCode,
                    "inventory_type" => $this->isDef ? $this->defInventoryType : $this->normalInventoryType,
                    "item_qty" => $this->sPackQty
                ]
            ];
            $package2_items = [$package2_item];

            if ($this->manyItem) {
                $package2_item21 = [
                    "package_item" => [
                        "order_item_id" => $this->orderItemId2,
                        "item_id" => $this->itemId2,
                        "item_code" => $this->itemCode2,
                        "inventory_type" => $this->isDef ? $this->defInventoryType : $this->normalInventoryType,
                        "item_qty" => $this->sPackQty
                    ]
                ];
                $package2_items[] = $package2_item21;
            }
        }

        $package_info = [
            "package_info" => [
                "tms_code" => "ZTO",
                "tms_order_code" => sprintf('ZTO%s%s', date('Ymd'), rand(1000, 9999)),
                "package_code" => sprintf('SHA%s%s', date('YmdHis'), strtoupper(Str::random(6))),
                "package_weight" => 145,
                "package_length" => 20,
                "package_width" => 80,
                "package_height" => 20,
                "package_item_list" => [
                    "cainiao_stock_out_bill_packageitemlist" => $package_items
                ]
            ],
        ];
        if ($this->manyPack) {
            $package_info2 = [
                "package_info" => [
                    "tms_code" => "ZTO",
                    "tms_order_code" => sprintf('ZTO%s%s', date('Ymd'), rand(1000, 9999)),
                    "package_code" => sprintf('SHA%s%s', date('YmdHis'), strtoupper(Str::random(6))),
                    "package_weight" => 145,
                    "package_length" => 20,
                    "package_width" => 80,
                    "package_height" => 20,
                    "package_item_list" => [
                        "cainiao_stock_out_bill_packageitemlist" => $package2_items
                    ]
                ],
            ];
        }
        $package_infos = [$package_info];
        if ($this->manyPack) {
            $package_infos[] = $package_info2;
        }

        $inventory_item = [
            "inventory_item" => [
                "batch_code" => (string)rand(100000000000000, 999999999999999),
                "produce_area" => "浙江杭州",
                "inventory_type" => $this->isDef ? $this->defInventoryType : $this->normalInventoryType,
                "item_qty" => $this->qty,
                "produce_code" => (string)rand(1000, 9999),
                'produce_date' => '',
                'due_date' => '',
            ],
        ];

        if ($this->itemCode == "SKUC00001") {
            $inventory_item['inventory_item']['produce_date'] = date("Y-m-d H:i:s", strtotime("-1 week"));
            $inventory_item['inventory_item']['due_date'] = date("Y-m-d H:i:s", strtotime("+8 weeks"));
        }

        if ($this->isHalf) {
            $inventory_item["inventory_item"]['item_qty'] -= $this->halfQty;
        }

        if ($this->isHalf) {
            $inventory_item2 = [
                "inventory_item" => [
                    "batch_code" => (string)rand(100000000000000, 999999999999999),
                    "produce_area" => "浙江杭州",
                    "inventory_type" => $this->isDef ? $this->normalInventoryType : $this->defInventoryType,
                    "item_qty" => $this->halfQty,
                    "produce_code" => (string)rand(1000, 9999),
                    'produce_date' => '',
                    'due_date' => '',
                ],
            ];

            if ($this->itemCode == "SKUC00001") {
                $inventory_item2['inventory_item']['produce_date'] = date("Y-m-d H:i:s", strtotime("-1 week"));
                $inventory_item2['inventory_item']['due_date'] = date("Y-m-d H:i:s", strtotime("+8 weeks"));
            }
        }

        $inventoryitemlist = [$inventory_item];
        if ($this->isHalf) {
            $inventoryitemlist[] = $inventory_item2;
        }

        $order_items = [
            "order_item" => [
                "order_item_id" => $this->orderItemId,
                "item_id" => $this->itemId,
                "item_code" => $this->itemCode,
                "apply_qty" => $this->qty,
                "inventory_item_list" => [
                    "cainiao_stock_out_bill_inventoryitemlist" => $inventoryitemlist
                ]
            ],
        ];
        if ($this->manyItem) {
            $inventory2_item = [
                "inventory_item" => [
                    "batch_code" => (string)rand(100000000000000, 999999999999999),
                    "produce_area" => "浙江杭州",
                    "inventory_type" => $this->isDef ? $this->defInventoryType : $this->normalInventoryType,
                    "item_qty" => $this->qty,
                    "produce_code" => (string)rand(1000, 9999),
                    'produce_date' => '',
                    'due_date' => '',
                ],
            ];

            if ($this->itemCode2 == "SKUC00002") {
                $inventory2_item['inventory_item']['produce_date'] = date("Y-m-d H:i:s", strtotime("-1 week"));
                $inventory2_item['inventory_item']['due_date'] = date("Y-m-d H:i:s", strtotime("+8 weeks"));
            }

            if ($this->isHalf) {
                $inventory2_item["inventory_item"]['item_qty'] -= $this->halfQty;
            }

            if ($this->isHalf) {
                $inventory2_item2 = [
                    "inventory_item" => [
                        "batch_code" => (string)rand(100000000000000, 999999999999999),
                        "produce_area" => "浙江杭州",
                        "inventory_type" => $this->isDef ? $this->normalInventoryType : $this->defInventoryType,
                        "item_qty" => $this->halfQty,
                        "produce_code" => (string)rand(1000, 9999),
                        'produce_date' => '',
                        'due_date' => '',
                    ],
                ];

                if ($this->itemCode2 == "SKUC00002") {
                    $inventory2_item2['inventory_item']['produce_date'] = date("Y-m-d H:i:s", strtotime("-1 week"));
                    $inventory2_item2['inventory_item']['due_date'] = date("Y-m-d H:i:s", strtotime("+8 weeks"));
                }
            }

            $inventoryitem2list = [$inventory2_item];
            if ($this->isHalf) {
                $inventoryitem2list[] = $inventory2_item2;
            }

            $order_items2 = [
                "order_item" => [
                    "order_item_id" => $this->orderItemId2,
                    "item_id" => $this->itemId2,
                    "item_code" => $this->itemCode2,
                    "apply_qty" => $this->qty,
                    "inventory_item_list" => [
                        "cainiao_stock_out_bill_inventoryitemlist" => $inventoryitem2list
                    ]
                ],
            ];
        }

        $cainiao_stock_out_bill_orderitemlist = [$order_items];
        if ($this->manyItem) {
            $cainiao_stock_out_bill_orderitemlist[] = $order_items2;
        }

        return [
            "wlb_wms_stock_out_bill_get_response" => [
                "stock_out_info" => [
                    "order_code" => !empty($orderCode) ? $orderCode : $this->orderCode,
                    "cn_order_code" => $cnOrderCode ? $cnOrderCode : sprintf("LBX%s%s", date('YmdHis'), (string)rand(100, 999)),
                    "order_type" => 903,
                    "confirm_time" => date("Y-m-d H:i:s"),
                    "status" => "ACCEPT",
                    "package_info_list" => [
                        "cainiao_stock_out_bill_packageinfolist" => $package_infos
                    ],
                    "order_item_list" => [
                        "cainiao_stock_out_bill_orderitemlist" => $cainiao_stock_out_bill_orderitemlist
                    ]
                ],
            ],
        ];
    }

    public function taobao_wlb_wms_sku_get(Request $request)
    {
        $ownerUserId = $request->get('owner_user_id', '');
        $itemCode = $request->get('item_code', '');
        $itemId = $request->get('item_id', '');
        return [
            "wlb_wms_sku_get_response" => [
                "bar_code" => $itemCode ? 'BAR' . $itemCode : 'BAR' . $itemId,
                "extend_fields" =>
                    "life_cycle_strong_control_class:0;sc_user_nick:香港克瑞斯供货商;en_name:La CASTA HEAD SPA SCALP BRUSH;source:pmc;isGlobalBatchMgt:0;hs_code:9615190090;",
                "gross_weight" => 140,
                "height" => 10,
                "iitem_code" => $itemCode,
                "is_area_sale" => true,
                "is_batch_mgt" => false,
                "is_danger" => false,
                "is_hygroscopic" => false,
                "is_shelflife" => false,
                "is_sn_mgt" => false,
                "item_id" => $itemId,
                "length" => 150,
                "name" => "La CASTA 蕾珂诗 头皮按摩梳",
                "net_weight" => 126,
                "request_id" => "16m7rf38k6dbj",
                "tag_price" => 0,
                "title" => "La CASTA 蕾珂诗 头皮按摩梳",
                "type" => "NORMAL",
                "use_yn" => false,
                "width" => 30,
                "wl_success" => true,
            ],
        ];
    }

    public function taobao_wlb_wms_stock_in_bill_get(Request $request)
    {
        $orderCode = $request->get('order_code', '');
        $cnOrderCode = $request->get('cn_order_code', '');

        $inventoryItem = [
            'inventory_item' => [
                "inventory_type" => $this->defInventoryType,
                "item_qty" => 12,
                "due_date" => "2015-08-20 13:51:59",
                "produce_date" => "2014-08-20 13:51:59",
                "produce_code" => "1234",
                "produce_area" => "浙江杭州",
                "batch_code" => "132323"
            ]
        ];

        $orderItem = [
            'order_item' => [
                "order_item_id" => "12332",
                "item_id" => $this->itemId2,
                "item_code" => "SF889900002",
                "apply_qty" => 1000,
                "inventory_item_list" => [
                    'cainiao_stock_in_bill_inventoryitemlist' => [$inventoryItem]
                ]
            ]
        ];

        return [
            'wlb_wms_stock_in_bill_get_response' => [
                'stock_in_info' => [
                    "order_code" => !empty($orderCode) ? $orderCode : $this->getDateTime(rand(1000, 9999)),
                    "cn_order_code" => !empty($cnOrderCode) ? $cnOrderCode : 'DEV' . $this->getDateTime(rand(1000, 9999)),
                    "order_type" => 904,
                    "confirm_time" => $this->getDateTime(),
                    "status" => "WMS_ACCEPT",
                    "order_item_list" => [
                        'cainiao_stock_in_bill_orderitemlist' => [$orderItem]
                    ]
                ]
            ]
        ];
    }

    public function taobao_wlb_wms_inventory_profitloss_get(Request $request)
    {
        $cnOrderCode = $request->get('cn_order_code', '');
        $orderItem = [
            'order_item' => [
                "item_code" => $this->itemCode2,
                "inventory_type" => $this->defInventoryType,
                "item_qty" => "12",
                "due_date" => "2015-08-20 14:08:07",
                "produce_date" => "2015-08-20 14:08:07",
                "produce_code" => "2820023",
                "produce_area" => "浙江杭州",
                "batch_code" => "12",
                "item_id" => $this->itemId2
            ]
        ];

        return [
            'wlb_wms_inventory_profitloss_get_response' => [
                'profit_loss_info' => [
                    "order_item_list" => [
                        'cainiao_inventory_profitloss_orderitemlist' => [
                            $orderItem
                        ]
                    ],
                    "store_code" => $this->storeCode,
                    "cn_order_code" => !empty($cnOrderCode) ? $cnOrderCode : 'DEV' . $this->getDateTime(rand(1000, 9999)),
                    "order_type" => 701,
                    "remark" => "aaa",
                    "created_time" => $this->getDateTime()
                ]
            ]
        ];
    }

    protected function getDateTime($str = '')
    {
        return !empty($str) ? date("YmdHis") . $str : date("Y-m-d H:i:s");
    }
}
